#-*- coding: utf-8 -*-
'''
@author: Sandro Sousa
@date: 14 de Outubro de 2013
@obs:
aula4 ex a
'''
import csv

#1)
with open('notas.csv', 'wb') as csvfile:
	escritor = csv.writer(csvfile)
	escritor.writerow(['Programacao 1', 10])
	escritor.writerow(['Programacao 2', 11])
	escritor.writerow(['Linguagens de Programacao', 17])

#2)
with open('notas.csv', 'rb') as csvfile:
	leitor = csv.reader(csvfile)
	k = 0; soma = 0.0
	for linha in leitor:
		soma += int(linha[1]); k += 1
	print "media = {:2.2f}".format(soma/k)

#3)
l = []
with open('notas.csv', 'rb') as csvfile:
	leitor = csv.reader(csvfile)
	for linha in leitor:
		l.append(linha[1])
print l


#4)
import sqlite3
conexao = sqlite3.connect('notas.db')
c = conexao.cursor()
c.execute('DROP TABLE IF EXISTS nota')
c.execute('CREATE TABLE nota (nome text, nota number)')
conexao.commit()
with open('notas.csv', 'rb') as csvfile:
	leitor = csv.reader(csvfile)
	for linha in leitor:
		c.execute('INSERT INTO nota VALUES (?,?)', linha)
conexao.commit()
conexao.close()

#5)
conexao = sqlite3.connect('notas.db')
c = conexao.cursor()
for linha in c.execute('SELECT * FROM nota'):
	print linha



